package com.example.springboot.excption;

import com.example.springboot.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 功能:
 * 作者：jian
 * 日期：2024/3/16 19:17
 */
@ControllerAdvice
public class GlobalException {

    @ExceptionHandler(ServiceException.class) //捕获异常
    @ResponseBody //将java对象转换为json
    public Result ServiceException(ServiceException e){
        return Result.error(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(Exception.class) //捕获异常
    @ResponseBody
    public Result globalException(Exception e){
        e.printStackTrace();
          return Result.error("500","系统错误");
    }

}   